/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DepositPanel.java
 *
 * Created on Nov 4, 2014, 3:06:45 PM
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.DepositDAO;
import com.ivanbiz.dao.impl.DepositDAOImpl;
import com.ivanbiz.model.Deposit;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.model.Pembayaran;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author IW20149X
 */
public class DepositRefundPanel extends javax.swing.JPanel {

    Kamar kamar;
    List<Deposit> listDeposit;
    JDialog jDialog;

    public DepositRefundPanel(JDialog jDialog) {
        initComponents();
        this.jDialog = jDialog;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        textKamar = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        textJumlah = new javax.swing.JTextField();
        buttonCari = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        buttonRefund = new javax.swing.JButton();
        buttonBatal = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Refund Deposit");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("Kamar :");

        textKamar.setEditable(false);

        jLabel3.setText("Jumlah :");

        textJumlah.setEditable(false);
        textJumlah.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textJumlahKeyReleased(evt);
            }
        });

        buttonCari.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/pencarian.jpg"))); // NOI18N
        buttonCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCariActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(textJumlah, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(textKamar, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCari)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(textKamar)
                    .addComponent(buttonCari, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textJumlah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonRefund.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/refund.jpg"))); // NOI18N
        buttonRefund.setText("Refund");
        buttonRefund.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRefundActionPerformed(evt);
            }
        });
        jPanel2.add(buttonRefund);

        buttonBatal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/batal.jpg"))); // NOI18N
        buttonBatal.setText("Batal");
        buttonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBatalActionPerformed(evt);
            }
        });
        jPanel2.add(buttonBatal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonRefundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRefundActionPerformed
        try {
            DepositDAO depositDAO = new DepositDAOImpl();
            System.out.println(kamar.getKos().getKodeKos() + kamar.getKodeKamar());
            depositDAO.refundDeposit(kamar.getKos().getKodeKos(), kamar.getKodeKamar());
            textJumlah.setText("");
            textKamar.setText("");
        } catch (Exception ex) {
            Logger.getLogger(DepositRefundPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonRefundActionPerformed

    private void textJumlahKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textJumlahKeyReleased
        try {
            Long.parseLong(textJumlah.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Format salah, harus angka", "warning", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_textJumlahKeyReleased

    private void buttonCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCariActionPerformed
        try {
            JDialog jDialogKos = new JDialog();
            jDialogKos.setModal(true);
            jDialogKos.setSize(800, 600);
            KamarPanel kamarPanel = new KamarPanel(jDialogKos, "deposit");
            jDialogKos.add(kamarPanel);
            jDialogKos.setLocationRelativeTo(null);
            jDialogKos.setVisible(true);
            this.kamar = kamarPanel.getKamarFasilitas();
            textKamar.setText(kamarPanel.getKamarFasilitas() == null ? null : kamarPanel.getKamarFasilitas().getKodeKamar());
            DepositDAO depositDAO = new DepositDAOImpl();
            Map map = new HashMap();
            map.put("kamar.id", kamar.getId());
            map.put("status", "0");
            Deposit deposit = (Deposit) depositDAO.getDataByEqualMore(Deposit.class, map);
            if (deposit == null) {
                JOptionPane.showMessageDialog(this, "Kamar ini belum pernah melakukan Deposit");
                textKamar.setText("");
            } else {
                textJumlah.setText(String.valueOf(new Double(deposit.getJumlah()).intValue()));
            }
        } catch (Exception ex) {
            Logger.getLogger(DepositRefundPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonCariActionPerformed

    private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBatalActionPerformed
        jDialog.dispose();
    }//GEN-LAST:event_buttonBatalActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBatal;
    private javax.swing.JButton buttonCari;
    private javax.swing.JButton buttonRefund;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField textJumlah;
    private javax.swing.JTextField textKamar;
    // End of variables declaration//GEN-END:variables

    private void validate(Deposit deposit) {
        try {
            DepositDAO depositDAO = new DepositDAOImpl();
            Pembayaran pembayaran = new Pembayaran();
            depositDAO.saveDeposit(deposit, pembayaran);
        } catch (Exception ex) {
            Logger.getLogger(DepositRefundPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
